const App = {
  mixins: [Query], //配置文件，路径/utils/mixins.js
  data() {
    return {
      treeData: [],
      formLabelWidth: "120px",
      totalMembers: 0,
      page: {
        activeName: "four",
        value1: "",
        value2: "",
        PageNumber: 1, //页码
        RowAmount: 10,
        Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%,%%"))),
        ExtensionCondition: window.btoa(unescape(encodeURIComponent(""))),
      },
      isShow: false,
      FlowType: "0",
    };
  },
  mounted() {
    this.gettotalMembers();
  },
  watch: {
    "dataForm.Village_Collective_SerialNumber"(val) {
      let str = this.dataForm.VillageNumber;
    },
  },
  computed: {
    colors() {
      return function (row) {
        let arr = [];
        if (row.Flow_Personal == "1") {
          arr.push("#F56C6C");
        } else if (row.Migrant_Personal == "1") {
          arr.push("#E6A23C");
        } else if (row.Migrant_Personal == "0") {
          arr.push("#67C23A");
        }
        if (arr.length == 1) {
          return { backgroundColor: arr[0] };
        } else {
          return { background: `linear-gradient(to right, ${arr.join(", ")})` };
        }
      };
    },
  },
  methods: {
    // 导出方法
    Batchexport() {
      const that = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        method: "post",
        data: {
          SQLSerialNumber: "s_base_sql_table_config",
          Condition: window.btoa(
            unescape(encodeURIComponent("%" + this.relativePath + "%"))
          ),
          ExtensionCondition: "",
        },
        success: (columns) => {
          console.log(columns);
          $.ajax({
            headers: requestHeaders(),
            method: "post",
            url: mh + "/generalBusiness/generalQueryBySQLSerialNumber",
            data: {
              Model_Url: this.relativePath,
              Condition: this.page.Condition,
              ExtensionCondition: this.page.ExtensionCondition || "",
            },
            success: (data) => {
              if (data.length > 0) {
                this.$confirm(
                  `当前共查出${data.length}条数据, 是否继续?`,
                  "提示",
                  {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "success",
                  }
                )
                  .then(() => {
                    console.log("点击了确认");

                    this.$prompt("请输入您导出的文件名", "提示", {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                    })
                      .then(({ value }) => {
                        this.$message({
                          type: "success",
                          message: "你的文件名是: " + value,
                        });
                        if (!value) {
                          value = "未知名";
                        }
                        // return;
                        const wb = XLSX.utils.book_new();
                        const ws_data = [columns.map((i) => i.COLUMN_COMMENT)];
                        const ws_main = data.map((item) =>
                          columns.map((row) => item[row.COLUMN_NAME])
                        );
                        const ws = XLSX.utils.aoa_to_sheet([
                          ...ws_data,
                          ...ws_main,
                        ]);
                        ws["!cols"] = [];
                        for (
                          let i = 0;
                          i < Object.keys(ws_data[0]).length;
                          i++
                        ) {
                          ws["!cols"].push({ wpx: 150 });
                        }
                        XLSX.utils.book_append_sheet(wb, ws, value);
                        XLSX.writeFile(wb, value + ".xlsx");
                      })
                      .catch(() => {
                        this.$message({
                          type: "info",
                          message: "取消输入",
                        });
                      });
                  })
                  .catch((erro) => {
                    console.log(erro);
                  });
              } else {
                this.$message({
                  message: "未查询出可导出的数据!",
                  type: "warning",
                });
              }
            },
            complete: (XMLHttpRequest) => {
              requestPretreatment(XMLHttpRequest);
            },
          });
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    // 导入数据方法
    import_data() {
      window.location.href = "../personal_information1/import.html";
    },
    handleClick() {
      if (this.page.activeName == "first") {
        this.page.ExtensionCondition = window.btoa(
          unescape(
            encodeURIComponent(
              "AND personal_information.RR_Village_Number like '232723%'  AND personal_information.Flow_Personal = '0'"
            )
          )
        );
      } else if (this.page.activeName == "second") {
        this.page.ExtensionCondition = window.btoa(
          unescape(
            encodeURIComponent(
              "AND personal_information.RR_Village_Number NOT like '232723%' AND personal_information.Flow_Personal = '0'"
            )
          )
        );
      } else if (this.page.activeName == "three") {
        this.page.ExtensionCondition = window.btoa(
          unescape(
            encodeURIComponent("AND personal_information.Flow_Personal = '1'")
          )
        );
      } else if (this.page.activeName == "four1") {
        this.page.ExtensionCondition = window.btoa(
          unescape(
            encodeURIComponent("AND personal_information.Influencers = '1'")
          )
        );
      } else {
        this.page.ExtensionCondition = window.btoa(
          unescape(encodeURIComponent(""))
        );
      }
      this.postgetList(this.page);
    },
    handleWH(row) {
      window.location.href =
        "../shop/influencers/add.html?action=Add&nodeKey=" + row.ID_Card_Number;
    },
    gettotalMembers() {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "personal_information",
          Condition: `RR_County_Nubmer='232723'`,
        },
        success: (res) => {
          this.totalMembers = res.length;
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
      });
    },
    batchFlow() {
      this.isShow = true;
      this.FlowType = "0";
    },
    batchCancelFlow() {
      this.isShow = true;
      this.FlowType = "1";
      this.page.activeName = "three";
      this.handleClick();
    },
    handleCopy(row) {
      let { primary_key } = JSON.parse(
        document.getElementById("table").getAttribute("data-options")
      );
      window.location.href =
        "./copy.html?action=Modify&nodeKey=" + row[primary_key];
    },
    handlePoints(row) {
      let { primary_key } = JSON.parse(
        document.getElementById("table").getAttribute("data-options")
      );
      window.location.href =
        "./points.html?action=Add&nodeKey=" + row[primary_key];
    },
    submitBatch() {
      this.$confirm(
        "是否将选中的人口信息" +
          (this.FlowType == "0" ? "标记为流动人口" : "流动人口标记取消") +
          "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let { TableName, primary_key } = JSON.parse(
            document.getElementById("table").getAttribute("data-options")
          );
          let res = [];
          let add = [];
          this.multipleSelection.forEach((item) => {
            let obj = deepClone(item);
            if (this.FlowType == "0") {
              obj.Flow_Personal = "1";
            } else if (this.FlowType == "1") {
              obj.Flow_Personal = "0";
            }
            res.push(obj);
            add.push({
              ID_Card_Number: item.ID_Card_Number,
              Flow_Personal: item.Flow_Personal,
              New_Flow_Personal: obj.Flow_Personal,
            });
          });
          let lJSONObject_DataSet = new Object();
          let lJSONObject_Table = new Object();
          lJSONObject_Table[TableName] = res;
          lJSONObject_DataSet["Modify"] = lJSONObject_Table;
          lJSONObject_DataSet["Add"] = {
            personal_flow_log: add,
          };
          let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
          if (!lJSONObject_DataSet["Add"]) {
            lJSONObject_DataSet["Add"] = new Object();
          }
          lJSONObject_DataSet["Add"]["base_operation_log"] = [
            {
              Operation_Type: "0",
              Operation_Path: this.relativePath,
              Operation_Main,
              Operation_Interface: "generalSave",
              Operation_IP: getIpAddress(),
            },
          ];
          submitSave(lJSONObject_DataSet, (res) => {
            this.$refs.multipleTable.clearSelection();
            this.isShow = false;
            if (res.Status == "success") {
              this.postgetList(this.page);
            } else {
              this.$message({
                type: "error",
                message: "操作失败",
              });
            }
          });
          // $.ajax({
          //   headers: requestHeaders(),
          //   url: `${mh}/generalBusiness/generalSave`,
          //   method: "post",
          //   data: {
          //     Data: JSON.stringify(lJSONObject_DataSet),
          //   },
          //   success: (res) => {
          //     this.$refs.multipleTable.clearSelection();
          //     this.isShow = false;
          //     if (res.Status == 'success') {
          //       this.postgetList(this.page);
          //     } else {
          //       this.$message({
          //         type: 'error',
          //         message: '操作失败'
          //       });
          //     }
          //   },
          //   complete: (XMLHttpRequest) => {
          //     requestPretreatment(XMLHttpRequest);
          //   },
          // });
        })
        .catch(() => {});
    },
    cancelBatch() {
      this.$refs.multipleTable.clearSelection();
      this.isShow = false;
    },
    //树的点击筛选事件
    handleNodeClick(data, node) {
      console.log(data.ProvinceNumber);
      this.page.Condition = window.btoa(
        unescape(
          encodeURIComponent(
            data.ProvinceNumber +
              "%,%%,%" +
              this.page.value1 +
              "%,%" +
              this.page.value2 +
              "%"
          )
        )
      );
      // if (node.level == 1) {
      //   this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + data.ProvinceNumber + "%,%%,%" + this.page.value1 + "%")))
      // } else {
      //   this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%" + data.ProvinceNumber + "%,%" + this.page.value1 + "%")))
      // }
      this.postgetList(this.page);
    },
    check() {
      this.page.Condition = window.btoa(
        unescape(
          encodeURIComponent(
            "%%,%%,%" + this.page.value1 + "%,%" + this.page.value2 + "%"
          )
        )
      );
      this.postgetList(this.page);
    },
    Treedata() {
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/clientOperation/getDistrictTree`,
        type: "GET",
        data: {
          DistrictCode: "232723",
        },
        success: (data) => {
          this.treeData = data; // 直接使用 this
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      // $.ajax({
      //   url: absolutepath() + "api/DistrictTree.json",
      //   type: "GET",
      //   success: function (data) {
      //     _this.treeData = data;
      //   },
      //   dataType: "json",
      // });
    },
    selectable(row) {
      return row.Flow_Personal == this.FlowType;
    },
    handleFlow(row) {
      this.$confirm(
        "是否将该人口信息" +
          (row.Flow_Personal == "0" ? "标记为流动人口" : "流动人口标记取消") +
          "?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let { TableName, primary_key } = JSON.parse(
            document.getElementById("table").getAttribute("data-options")
          );
          $.ajax({
            headers: requestHeaders(),
            url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
            method: "post",
            data: {
              Data: TableName,
              Condition: primary_key + "='" + row[primary_key] + "'",
            },
            success: (res) => {
              if (res.length == 1) {
                res[0].Flow_Personal = row.Flow_Personal == "0" ? "1" : "0";
                let lJSONObject_DataSet = new Object();
                let lJSONObject_Table = new Object();
                lJSONObject_Table[TableName] = res;
                lJSONObject_DataSet["Modify"] = lJSONObject_Table;
                lJSONObject_DataSet["Add"] = {
                  personal_flow_log: [
                    {
                      ID_Card_Number: row.ID_Card_Number,
                      Flow_Personal: row.Flow_Personal,
                      New_Flow_Personal: row.Flow_Personal == "0" ? "1" : "0",
                    },
                  ],
                };
                let Operation_Main = JSON.stringify(
                  deepClone(lJSONObject_DataSet)
                );
                if (!lJSONObject_DataSet["Add"]) {
                  lJSONObject_DataSet["Add"] = new Object();
                }
                lJSONObject_DataSet["Add"]["base_operation_log"] = [
                  {
                    Operation_Type: "0",
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress(),
                  },
                ];
                submitSave(lJSONObject_DataSet, (res) => {
                  if (res.Status == "success") {
                    this.postgetList(this.page);
                  } else {
                    this.$message({
                      type: "error",
                      message: "操作成功",
                    });
                  }
                });
                // $.ajax({
                //   hheaders: requestHeaders(),
                //   url: `${mh}/generalBusiness/generalSave`,
                //   method: "post",
                //   data: {
                //     Data: JSON.stringify(lJSONObject_DataSet),
                //   },
                //   success: (res) => {
                //     if (res.Status == 'success') {
                //       this.postgetList(this.page);
                //     } else {
                //       this.$message({
                //         type: 'error',
                //         message: '操作成功'
                //       });
                //     }
                //   },
                //   complete: (XMLHttpRequest) => {
                //     requestPretreatment(XMLHttpRequest);
                //   },
                // });
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败",
                });
              }
            },
            complete: (XMLHttpRequest) => {
              requestPretreatment(XMLHttpRequest);
            },
          });
        })
        .catch(() => {});
    },
    personalAdd() {
      window.location.href = "./personalAdd.html?action=Add";
    },
    //编辑按钮
    handleEdit(row) {
      let { primary_key } = JSON.parse(
        document.getElementById("table").getAttribute("data-options")
      );
      window.location.href =
        "./personalAdd.html?action=Modify&nodeKey=" + row[primary_key];
    },
  },
};
const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
